/*
 *接口编写处...
*如果接口需要做Action的权限验证，请在Action上使用属性
*如: [ApiActionPermission("cw_djlc_cwzb_newenergy_view",Enums.ActionPermissionOptions.Search)]
 */
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using VOL.Entity.DomainModels;
using VOL.Finance.IServices;
using VOL.Core.Filters;
using System.Net;
using VOL.Core.Utilities;
using VOL.Core.DBManager;
using VOL.Core.Extensions;

namespace VOL.Finance.Controllers
{
    public partial class cw_djlc_cwzb_newenergy_viewController
    {
        private readonly Icw_djlc_cwzb_newenergy_viewService _service;//访问业务代码
        private readonly IHttpContextAccessor _httpContextAccessor;

        [ActivatorUtilitiesConstructor]
        public cw_djlc_cwzb_newenergy_viewController(
            Icw_djlc_cwzb_newenergy_viewService service,
            IHttpContextAccessor httpContextAccessor
        )
        : base(service)
        {
            _service = service;
            _httpContextAccessor = httpContextAccessor;
        }
        WebResponseContent webResponse = new WebResponseContent();
        [HttpPost, Route("update")]
        [ApiActionPermission()]
        public override ActionResult Update([FromBody] SaveModel saveModel)
        {
            var vehiclekeyword = saveModel.MainData["vehiclekeyword"];
            var cw_djlc_djzb_id = saveModel.MainData["cw_djlc_djzb_id"];
            if (cw_djlc_djzb_id .IsNullOrEmpty() || vehiclekeyword.IsNullOrEmpty())
            {
                return Json(webResponse.Error("数据不能为空！"));
            }
            var dapper = DBServerProvider.SqlDapper;
            try
            {
                dapper.BeginTrans();
              
                if (cw_djlc_djzb_id != null)
                {
                    string sql = "update cw_djlc_cwzb_newenergy_view set [cw_djlc_cwzb_id] = @vehiclekeyword where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql, new { vehiclekeyword , cw_djlc_djzb_id });
                }
               
                dapper.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                dapper.Rollback();
                return Json(webResponse.Error("编辑失败！"));
            }
            return Json(webResponse.OK("编辑成功！"));
        }
        [HttpPost, Route("update1")]
        [ApiActionPermission]
        public ActionResult update1(string development_expense, string vehiclelogisticsfee, string dcjb, string factoryprice, string cw_djlc_djzb_id,string product_desc)
        {
            var dapper = DBServerProvider.SqlDapper;
            try
            {
                dapper.BeginTrans();
                string sql, sql2 = "";
                //这么写要求边际率 出厂价格不能为空了
                if (dcjb == null || "null".Equals(dcjb))
                {
                    sql = "update cw_djlc_djzb set [dcjb] = null where cw_djlc_djzb_id = @djzbId";
                    dapper.ExcuteNonQuery(sql, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql = "update cw_djlc_djzb set [dcjb] = @dcjb where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql, new { dcjb, cw_djlc_djzb_id });
                } 
                if ("null".Equals(factoryprice) || "".Equals(factoryprice))
                {
                    sql2 = "update cw_djlc_djzb set [factoryprice] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [factoryprice] = @factoryprice where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { factoryprice, cw_djlc_djzb_id });
                }
                if ("null".Equals(vehiclelogisticsfee) || "".Equals(vehiclelogisticsfee))
                {
                    sql2 = "update cw_djlc_djzb set [vehicle_logistics_fee] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [vehicle_logistics_fee] = @vehiclelogisticsfee where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { vehiclelogisticsfee, cw_djlc_djzb_id });
                }

                if ("null".Equals(development_expense) || "".Equals(development_expense))
                {
                    sql2 = "update cw_djlc_djzb set [development_expense] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [development_expense] = @development_expense where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { development_expense, cw_djlc_djzb_id });
                }
                if ("null".Equals(product_desc) || "".Equals(product_desc))
                {
                    sql2 = "update cw_djlc_djzb set [product_desc] = null where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { cw_djlc_djzb_id });
                }
                else
                {
                    sql2 = "update cw_djlc_djzb set [product_desc] = @product_desc where cw_djlc_djzb_id = @cw_djlc_djzb_id";
                    dapper.ExcuteNonQuery(sql2, new { product_desc, cw_djlc_djzb_id });
                }
                dapper.Commit();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                dapper.Rollback();
                return Json(webResponse.Error("保存失败！"));
            }
            return Json(webResponse.OK("保存成功！"));
        }
    }

}
